4.3 Graph

Graph allows you to display data graphically. It is a very versatile type of Objects and its usage highly depends on the imagination of the programmer. Graph class methods allow to put on the display lines or dots, draw arrays or math functions as curves or histograms in both linear or logarithmic scales or mixed.

* Type parameters XType and YType. Here we have used the new feature of C++ - class templates. Class Graph is in effect a template. Type parameters XType and YType are for type of values on the x-and y-axes. They can take the next values: double, float, long, int, unsigned long, unsigned int, char, unsigned char. When declaring the instance of Graph you have to define the types of axes.

Example:

   Graph<int,double>  grFourier( ..... );

It means that you can display on the grFourier array of double values over integer parameter or double function with integer parameter.

Constructor:

Graph ( // —- ALL SIZES IN VGA PIXELS ——
int x0,int y0, // where to paint graph
objtype type, // may be FIXED, PERM, POPUP
char * title, // title of graph
int xtics,int ytics, // number of tic marks
int xpixtic,int ypixtic,// number of pixels between tics
float xval0,float yval0,// values of x,y (LIN) or powers of 10 (LOG) at origin
float xticinc,float yticinc,// increment of tick labels only for LIN scales
//———— Hereafter are defaults ——-
// procedure supplied by programmer to repaint Graph
void (far *paintproc)()=procNULL,
char * xtitle = "",char * ytitle = "",// axes titles
// format of tic labels. ! Can be only float %[..]f !
char * xticfmt = "",char * yticfmt = "",
int xscale=LIN, int yscale=LIN,// type of scale, may be LIN or LOG
int frame=FRAMED, // display may FRAMED or UNFRAMED
// fonts for
int titlefont=0,int titlefontsize=1, // title
int axtitlefont=0,int axtitlefontsize=1,// axes titles
int ticfont=SMALL_FONT,int ticfontsize=2,// tic labels
// color schemes for
graphcolors graphcolconfig=YellowOnCyan, // display
plaquecolors framecolconfig=plaquecoldflt, // frame
int gridstyle=0x5555); // style of grid lines

Creates Graph with specified parameters. Number of tic marks on the axes is defined by parameters xtics and ytics. The distance between tic marks in VGA pixels is controlled by xpixtic and ypixtic. The type of scale of x or y axes may be of two kinds - linear (LIN) or logarithmic (LOG). In other words we say that x or y axes are in LIN or LOG mode. These modes are set by parameters xscale and yscale when calling the constructor and cannot be changed later. For linear scale you can define the difference between neighbouring tic labels, we call it tic label increment, by xticinc or yticinc parameters. For logarithmic scales these parameters are senseless because the difference in this case is pre-defined as a power 1 of 10, so xticinc or yticinc can take any value.

If the axis is in LIN mode, then the value of displayed data at the origin of graph ( the origin is the LEFT-BOTTOM corner of Graph's display ) is defined by xval0 or yval0 as a float point number. If the scale is logarithmic, then these parameters define the power of 10 at the origin. It may be a positive or negative integer. The xtitle and ytitle parameters supply the titles alongside the axes. The font for these titles is defined by axtitlefont and axtitlefontsize. You can also customize the font style for tic labels redefining parameters ticfont and ticfontsize. Style of grid lines also may be customized by programmer. The gridstyle parameter provides the bit pattern for grid lines.
Example:

Graph<int,float>
 grFourier( 0, 0, FIXED, "Fourier transform",   // title
            24, 18,      // number of tic marks
            20, 20,      // number of pixels between tics
            0, min_y,    // values of x,y at origin
            (NumPoints+1)/24., (max_y-min_y)/18.,// increment of tick labels
            procDummy,         // no paint procedure
            "Frequency","Amplitude",// axes titles
            "%.0f","%.1f");    // format of tic labels

Data members:

All are inherited from the object base class (sect.4).

Methods:

Includes methods inherited from the object base class (sect.4).

virtual void Paint(void);

Paints Graph Object with empty display.

virtual void cls();

Clears the display (we also call it paper), removing all lines, curves and etc. and repainting the grid.

void DrawGrid(void);

Repaints the grid lines.

void SetInk( COLORS inkcolor);

Sets default color of ink. It is a color used to draw dots, lines, curves, fill rectangles on the graph, when the color parameter is not specified for the drawing method.

* The color parameter. In the following methods color=-1 means that if you omit color parameter default ink color will be used. If you don't omit color parameter then you can specify any of BGI colors.

//— DOT

void PutDot( double x, double y, int color=-1 );

Paints a single pixel on the paper of graph. The x and y are the co-ordinates of pixel and must be defined in units of x and y axes.
Example:

 for ( i=0; i<NumPoints; i++)
    grFourier.PutDot( i, y[i], YELLOW);

//— LINE

void PutLine( double x0, double y0,
double x1, double y1, int color=-1 );

Paints the line on the paper of graph. The line connects points (x0,y0) and (x1,y1) defined in units of x- and y- axes.
Example:

 for ( i=0; i<NumPoints-1; i++)
    grFourier.PutLine( i, y[i], i+1, y[i+1] );

//— FUNCTION

** Note that the function cannot have negative values if y-axis was set to LOG mode.

void ShowCurve( int numpoints, YType (far *func)(XType), int color=-1 );

Draws math function func with one parameter as a curve. The numpoints defines the number of samples. More samples means smoother curve and longer time of drawing. Function is scanned equidistantly from the origin of x-axis to its end.

void HideCurve( int numpoints, YType (far *func)(XType) );

Is opposite to ShowCurve - clears out the curve from paper and redraws the grid.

//— ARRAY

** Note that array elements cannot have negative values if y-axis was set to LOG mode.

void ShowCurve( int numpoints, XType x0i, XType xmaxi,
YType far * array, int color=-1);

Shows array as a curve on the graph. The numpoints is the number of elements of array. The x0i and xmaxi are values on the x-axis associated with correspondingly first and last elements of array. The elements of array are drawn equidistantly along x-axis. Parts of the curve lying behind Graph's display are cut out.
Example:
grFourier.ShowCurve( NumPoints, 0, NumPoints-1, y, YELLOW);

void HideCurve(int numpoints, XType x0c, XType xmaxc,
YType far * array );

Is opposite to ShowCurve - clears out the curve from paper and redraws the grid.

void ShowHistogram( int numpoints, XType x0i, XType xstep,
YType far * array, int color=-1 );

** If x-axis has been set to LOG mode, calling ShowHistogram method interrupts the program with the error message:
Graph::ShowHistogram : X-scale cannot be LOG for histogram

Shows array as a histogram. The numpoints is the number of elements of array. The x0i is the value on the x-axis associated with the first element of array. The distance between neighbouring bars of histogram is defined by xstep parameter (in the unit of x-axis). The width of bar is calculated automatically.
Example:

grFourier.ShowHistogram( NumPoints, 0, 1023 , y );

void HideHistogram( int numpoints, XType x0a, XType xstep,
YType far * array, int color=-1 );

Is opposite to ShowHistogram - clears out the histogram from the paper and redraws the grid.

//— TWO ARRAYS

** Note that array elements cannot have negative values if axis was set to LOG mode.

void ShowCurve(int numpoints, XType far *Xdata,
YType far *Ydata, int color);

Arrays must have equal number of elements. Xdata and Ydata arrays provide coordinates of points of the curve on the graph. The numpoints is the number of elements of arrays. Parts of the curve lying behind Graph's display are cut out.

void HideCurve(int numpoints, XType far *Xdata,
YType far *Ydata);

Is opposite to ShowCurve - clears out the curve from the paper and redraws the grid.

Color schemes:

There are four pre-defined color patterns for Graph:

struct graphcolors            // Graph color structure
{ COLORS
   Paper,      Ink, Grid,      Title, AxesTitles, TicLabels ;};
const graphcolors
WhiteOnCyan =
  { CYAN,    WHITE, LIGHTCYAN, RED, BLACK,         BLUE },
YellowOnCyan =
  { CYAN,     YELLOW, LIGHTCYAN, RED, BLACK,       BLUE };
GreenOnBlue =
  { BLUE, LIGHTGREEN, LIGHTBLUE, RED, BLACK,       BLUE },
YellowOnBlue =
  { BLUE,     YELLOW, LIGHTBLUE, RED, BLACK,       BLUE };}